import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/components/network_image_load/image_utils.dart';
import 'package:yt_devops/components/text_width.dart';
import 'package:yt_devops/components/my_gesture_detector.dart';
import 'package:yt_devops/resources/str_res_keys.dart';

import '../../../../../components/gaps.dart';
import '../../../../../components/yt_text_style.dart';
import '../../../../../styles/styles.dart';

class ItemWidget extends StatelessWidget {
  final String title;
  final TextEditingController? textFieldController; //输入框
  final bool isRequired; //是否为必填
  final Color titleColor; //title的颜色
  final Color inputTextColor; //输入文字颜色
  final Color unitTextColor; //单位文字颜色
  final double titleFont; //title字体大小
  final double inputTextFont; //title字体大小
  final bool isShowDivider; //是否展示分割线
  final bool isShowRight; //是否右侧箭头,如果不展示则不可点击
  final String rightImagePath; //右侧图片路径,默认是右箭头
  late String hintString; //占位文字
  final String unitString; //最后面单位,kW,kWh这些,
  final TextInputType? textInputType; //输入键盘类型
  final List<TextInputFormatter>? inputFormatters;
  final num? inputTextWidth;
  final bool isCanEdit; //是否可以响应输入
  final bool isShowText; //是否只展示text,不展示TextField,解决一行或者两行文字,居中问题
  final int maxLines; //最大行数
  final Function()? clickCallBack;
  final Function(String)? textChangeCallBack;

  ItemWidget(this.title, this.textFieldController,
      {this.isRequired = false,
      this.isShowDivider = true,
      this.isShowRight = false,
      this.titleColor = Colour.mainTextColor,
      this.inputTextColor = Colour.otherTextColor,
      this.unitTextColor = Colour.otherTextColor,
      this.rightImagePath = 'assets/images/right_arrow.png',
      this.hintString = '',
      this.textInputType = TextInputType.text,
      this.inputTextWidth = 150,
      this.inputFormatters,
      this.unitString = '',
      this.clickCallBack,
      this.titleFont = 14,
      this.inputTextFont = 14,
      this.isCanEdit = false,
      this.isShowText = false,
      this.textChangeCallBack,
      this.maxLines = 1,
      Key? key})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    double unitWidth = 0;
    if(unitString.isNotEmpty){
      unitWidth = TextWidth.calculateTextSize(unitString, ytTextStyle(
        fontSize: inputTextFont,
        fontWeight: FontWeight.normal,
        color: unitTextColor,
        height: 1,
      ));
    }

    if (hintString.isEmpty) {
      hintString = ResKey.plsInput.tr;
    }
    // Log.d('inputTextWidth:$inputTextWidth');
    var valueWidth = TextWidth
        .calculateTextSize(hintString, textFieldController!.text.isEmpty
        ? ytTextStyle(
      fontSize: 14,
      color: Colour.lightTextColor,
      fontWeight: FontWeight.normal,
    )
        : ytTextStyle(
      fontSize: 14,
      color: inputTextColor,
      height: 1.2,
    ));
    // Log.d('inputTextWidth:$inputTextWidth');
    // Log.d('isShowText:$isShowText');

    return Column(
      children: [
        Container(
          color: Colors.white,
          height: 49,
          child: Row(
            children: [
              isRequired
                  ? Container(
                      alignment: Alignment.center,
                      margin: EdgeInsets.only(
                        left: 6.w,
                        right: 2.w,
                      ),
                      child: Text(
                        '*',
                        style: ytTextStyle(
                          color: Colour.warningTextColor,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                    )
                  : Gaps.hGap16,
              Container(
                // color: Colors.red,
                width: ScreenUtil().screenWidth - ScreenUtil().setWidth(inputTextWidth!) - 65.w - unitWidth -14.w,
                child: Text(
                  title,
                  maxLines: 2,
                  style: ytTextStyle(
                    fontSize: titleFont,
                    fontWeight: FontWeight.normal,
                    color: titleColor,
                  ),
                ),
              ),
              const Spacer(),
              MyGestureDetector(
                  behavior: HitTestBehavior.opaque,
                  onTap: () {
                    if (clickCallBack != null) {
                      clickCallBack!();
                    }
                  },
                  child: Container(
                    alignment: Alignment.centerRight,
                    color: Colors.white,
                    width: ScreenUtil().setWidth(inputTextWidth!),
                    child: isShowText
                        ? Text(
                            textFieldController!.text.isNotEmpty?textFieldController!.text:hintString,
                            maxLines: 2,
                            textAlign: TextAlign.right,
                            overflow: TextOverflow.ellipsis,
                            style: textFieldController!.text.isEmpty
                                ? ytTextStyle(
                                    fontSize: inputTextFont,
                                    color: Colour.hintTextColor,
                                    fontWeight: FontWeight.normal,
                                  )
                                : ytTextStyle(
                                    fontSize: inputTextFont,
                                    color: inputTextColor,
                                    height: 1.2,
                                  ),
                          )
                        : TextField(
                            textAlign: TextAlign.right,
                            // textAlignVertical:TextAlignVertical.center,
                            maxLines: maxLines,
                            style: ytTextStyle(
                              fontSize: inputTextFont,
                              color: inputTextColor,
                              height: 1.2,
                            ).copyWith(textBaseline: TextBaseline.alphabetic),
                            controller: textFieldController,
                            keyboardType: textInputType,
                            inputFormatters: inputFormatters,
                            decoration: InputDecoration(
                              border: InputBorder.none,
                              contentPadding:
                                  const EdgeInsets.only(top: 0, bottom: 0),
                              hintText: hintString,
                              enabled: isCanEdit,
                              hintStyle: ytTextStyle(
                                fontSize: inputTextFont,
                                color: Colour.hintTextColor,
                                fontWeight: FontWeight.normal,
                              ),
                            ),

                            onChanged: (String str) {
                              if (textChangeCallBack != null) {
                                textChangeCallBack!(str);
                              }
                            },
                          ),
                  )),
              Visibility(
                visible: unitString.isNotEmpty,
                child: Padding(
                  padding: EdgeInsets.only(left: 4.w),
                  child: Text(
                    unitString,
                    style: ytTextStyle(
                      fontSize: inputTextFont,
                      fontWeight: FontWeight.normal,
                      color: unitTextColor,
                      height: 1,
                    ),
                  ),
                ),
              ),
              Visibility(
                visible: isShowRight,
                child: GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  onTap: () {
                    if (clickCallBack != null) {
                      clickCallBack!();
                    }
                  },
                  child: Image.asset(
                    rightImagePath,
                    width: 16.w,
                    height: 16.w,
                  ),
                ),
              ),
              Gaps.hGap16,
            ],
          ),
        ),
        Visibility(
          visible: isShowDivider,
          child: Container(
            height: 0.5,
            color: Colour.dividerColor,
           margin: EdgeInsets.only(left: 16.w,right: 16.w),
          ),
        ),
      ],
    );
  }
}
